using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace Spacewide
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Spacewide1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Sprite log;
        Sprite login;
        Sprite mdp;
        Sprite connect;
        Sprite fond;

        //KeyboardState keystates;
        MouseState mousestates;

        byte _login_condition;

        int hauteur;
        int largeur;

        public Spacewide1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Load all content within a certain folder. The function
        /// returns a dictionary where the file name, without type
        /// extension, is the key and the texture object is the value.
        ///
        /// The contentFolder parameter has to be relative to the
        /// game.Content.RootDirectory folder.
        /// </summary>
        /// <typeparam name="T">The content type.</typeparam>
        /// <param name="contentManager">The content manager for which content is to be loaded.</param>
        /// <param name="contentFolder">The game project root folder relative folder path.</param>
        /// <returns>A list of loaded content objects.</returns>
        public Dictionary<String, T> LoadContentByFolde<T>(string contentFolder)
        {
            //Load directory info, abort if none
            DirectoryInfo dir = new DirectoryInfo(Content.RootDirectory + "\\" + contentFolder);
            if (!dir.Exists)
                throw new DirectoryNotFoundException();
            //Init the resulting list
            Dictionary<String, T> result = new Dictionary<String, T>();

            //Load all files that matches the file filter
            FileInfo[] files = dir.GetFiles("*.*");
            foreach (FileInfo file in files)
            {
                string key = Path.GetFileNameWithoutExtension(file.Name);

                result[key] = Content.Load<T>(Content.RootDirectory + "/" + contentFolder + "/" + key);
            }
            //Return the result
            return result;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.graphics.IsFullScreen = false;
            this.graphics.PreferredBackBufferWidth = largeur = 1000;
            this.graphics.PreferredBackBufferHeight = hauteur = 700;
            this.graphics.ApplyChanges();

            this.Window.Title = "Spacewide Log In";

            this.Window.AllowUserResizing = false;

            //*
            log = new Sprite();
            log.Initialize();
            login = new Sprite();
            login.Initialize();
            mdp = new Sprite();
            mdp.Initialize();
            connect = new Sprite();
            connect.Initialize();
            // */
            fond = new Sprite();
            fond.Initialize();


            log.Position = login.Position = mdp.Position = new Vector2(0,hauteur - 151);
            connect.Position = new Vector2(416, (float)(hauteur - 144.5));

            _login_condition = 0;

            this.IsMouseVisible = true;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            StreamReader monStreamReader = new StreamReader("rand_menu.txt");
            List<int> nb = new List<int>(3);
            while(monStreamReader.EndOfStream != true)
                nb.Add(Convert.ToInt32(monStreamReader.ReadLine()));

            monStreamReader.Close();
            int f = 0;
            Random o = new Random(new Random().Next(0, 1500000));

            while (nb.Contains(f = o.Next(1, 10))) ;

            fond.LoadContent(Content, "f"+f.ToString());

            StreamWriter msW = new StreamWriter("rand_menu.txt", false);
            msW.WriteLine(f);
            msW.WriteLine(nb[2]);
            msW.WriteLine(nb[1]);
            msW.Close();

            log.LoadContent(Content, "login");
            login.LoadContent(Content, "logsurbri");
            mdp.LoadContent(Content, "passsurbri");
            connect.LoadContent(Content, "cons");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            //keystates = Keyboard.GetState();
            mousestates = Mouse.GetState();

            if (mousestates.LeftButton == ButtonState.Pressed)
            {
                if (mousestates.X >= 26 && mousestates.X <= 203 && mousestates.Y >= hauteur - 125 && mousestates.Y <= hauteur - 97)
                    _login_condition = 1;
                else if (mousestates.X >= 26 && mousestates.X <= 203 && mousestates.Y >= hauteur - 54 && mousestates.Y <= hauteur - 26)
                    _login_condition = 2;
                else
                    _login_condition = 0;
            }

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            
            fond.Draw(spriteBatch, gameTime);

            if (_login_condition == 0)
                log.Draw(spriteBatch, gameTime);
            else if (_login_condition == 1)
                login.Draw(spriteBatch, gameTime);
            else
                mdp.Draw(spriteBatch, gameTime);
            
            connect.Draw(spriteBatch, gameTime);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
